const schema =  require('../conf/schema');
const { OK, ERR } = require('../util/util');
const ObjectID = require('mongodb').ObjectID;
const Formats = require('../util/formats');

const Banner = schema.banner;

const category = {
  init: function() {
    Banner.count({}, function (err, count){
      console.log('total recored: ', count);
      if(count == 0) {
        const data = [{
          "isLock": false,
          "name": "rose",
          "figure": "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/171757bed3ba8f2906a06ddb9785fe22",
          "color": "",
          "price": 5,
          "imageId": "5a65a26da1a970000180deb4",
          "deepLink": "easydrawingstepbystep:\/\/drawingactivity\/drawing?detail_drawing_url= 5a65a26ea1a970000180e0b4",
          "id": "5affcf7a3a1e9e0001e17fa2"
        }, {
          "isLock": false,
          "name": "pixelart_2",
          "figure": "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/c63c6e25516e4449fcdd01e0d7106434",
          "color": "",
          "price": null,
          "imageId": "",
          "deepLink": "market:\/\/details?id=sandbox.pixel.number.coloring.book.page.art.free&referrer=utm_source%3DEasyDrawingBanner%26utm_medium%3Dcpi",
          "id": "5affd3323a1e9e0001e17fa3"
        }];

         console.log('init banner: ', data);
         data.forEach(i => {
           const banner = new Banner({
             isLock: i.isLock,
             name: i.name,
             figure: i.figure,
             color: i.color,
             price: i.price,
             imageId: i.imageId,
             deepLink: i.deepLink,
             id: i.id
           });
           banner.save();
         });
       }
    });

  },
  findAll: function() {
    return Banner.find({}, null, {sort: {id: -1}}, function (err, items) {
        if (err) {
            console.error(err);
            return ERR(500, err);
        } else {
            return items;
        }
    });
  }
};

module.exports = category;
